<template>
	<view>
		<uni-popup ref="popup" type="center">
			<view class="login_content">
				<text class="tip">您还没有登录哦~</text>
				<button class="login_btn" open-type="getUserInfo" @getuserinfo="goWxLogin">登录</button>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uniPopup from './uni-popup/uni-popup.vue';
	import {wxLogin,wxLoginGetUserInfo} from '@/Service/login.js';
	export default {
		components: {
			uniPopup
		},
		data() {
			return {
				
			}
		},
		mounted() {
		
		},
		computed: {
		    userNoLogin() {
		      return this.$store.state.noLogin
		    }
		},
		watch: {
			//监听到用户处于未登录状态
			userNoLogin(e) {
				this.open()
			}
		},
		methods: {
			open(){
				this.isHavePower(() => {
					this.$refs.popup.open();
				});
			},
			colse(){
				this.$refs.popup.close();
			},
			token(){
				uni.setStorage({
					key: "token",
					data: ""
				});
			},
			
			//判断用户是否授权登录
			isHavePower(callback) {
				let self = this
				uni.getSetting({
					success: (res) => {
						console.log("授权状态", res);
						if (res.authSetting['scope.userInfo']) { //用户已授权,
							// self.goWxLoginGetUserInfo(userDate)
							uni.getUserInfo({
								success(userDate) {
									console.log("已授权", userDate);
									self.goWxLoginGetUserInfo(userDate);
								},
							});
						} else { //用户还未授权过
							console.log('未授权')
							callback && callback();
						}
					}
				})
			},
			
			
			//普通登录
			goWxLogin(e){
				let self = this
				let userDate = e.mp.detail //用户授权通过以后，获取到的用户昵称等信息
				if (userDate.rawData){
					console.log('用户按了允许按钮')
					uni.showLoading({
					    title: '登录中'
					});
					self.goWxLoginGetUserInfo(userDate)
					
				} else {
					console.log('用户按了拒绝按钮')
					self.$emit("login")
				}
				self.colse()
			},
			
			//登录获取用户数据
			goWxLoginGetUserInfo(userDate){
				let self = this
				uni.login({
					provider:"weixin",
					success:(login_ret => {
						let params = {
							code:login_ret.code,
							signature:userDate.signature,
							rawData:userDate.rawData,
							encryptedData:userDate.encryptedData,
							iv:userDate.iv
						}
						wxLoginGetUserInfo(params).then((ret) => {
							console.log(JSON.stringify(ret))
							uni.setStorageSync('token',ret.data.data.token)
							uni.setStorageSync('avatar',ret.data.data.avatar)
							uni.setStorageSync('nickname',ret.data.data.nickname)
							let userInfo = {
								avatar:ret.data.data.avatar,
								nickname:ret.data.data.nickname,
								phone:ret.data.data.phone,
							}
							this.$store.dispatch('userInfo',userInfo)
							uni.hideLoading();
							self.ToastSuccess('登录成功')
						}).catch((err) => {
							console.log(err)
						})
					})
				})
				self.$emit("login",'点击登录')
				self.colse()
				
			},
			//普通toast
			Toast(e){
				uni.showToast({
				    title: e,
					icon:'none',
				    duration: 2000
				});
			},
			ToastSuccess(e){
				uni.showToast({
				    title: e,
					icon:'success',
				    duration: 2000
				});
			}
		}
	}
</script>

<style>
	.login_content{
		width:500upx;
		height:333upx;
		background:rgba(255,255,255,1);
		border-radius:10upx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	.tip{
		display: block;
		font-size: 30upx;
		color: #333333;
	}
	.login_btn{
		width:240upx;
		height:66upx;
		background-color:rgba(165,118,247,1);
		border-radius:43upx;
		line-height: 66upx;
		font-size: 30upx;
		color: #FFFFFF;
		margin-top: 80upx;
	}
</style>
